using System;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Document;

namespace Peter.Logic.AvalonEdit
{
   /// <summary>
   /// Represents a text marker in a document.
   /// </summary>
   public sealed class TextMarker : TextSegment
   {
      private Color? m_ForegroundColor;
      private Color? m_BackgroundColor;
      private readonly TextMarkerService m_Service;

      /// <summary>
      /// Initializes a new text marker.
      /// </summary>
      /// <param name="service">Text marker service.</param>
      /// <param name="startOffset">The start offset of the text segment.</param>
      /// <param name="length">The length of the text segment.</param>
      public TextMarker (TextMarkerService service, int startOffset, int length)
      {
         if (service == null)
            throw new ArgumentNullException ("service");
         this.m_Service = service;
         this.StartOffset = startOffset;
         this.Length = length;
      }

      /// <summary>
      /// Event occurs when the marker is deleted.
      /// </summary>
      public event EventHandler Deleted;

      /// <summary>
      /// Gets if this marker is deleted or not.
      /// </summary>
      public bool IsDeleted
      {
         get { return !this.IsConnectedToCollection; }
      }

      /// <summary>
      /// Deletes the marker.
      /// </summary>
      public void Delete ()
      {
         this.m_Service.Remove (this);
      }

      /// <summary>
      /// Used to notify when deleted.
      /// </summary>
      internal void OnDeleted ()
      {
         if (this.Deleted != null)
            this.Deleted (this, EventArgs.Empty);
      }

      /// <summary>
      /// Redraws this marker.
      /// </summary>
      void Redraw ()
      {
         this.m_Service.Redraw (this);
      }

      /// <summary>
      /// Gets or Sets the background color of the marker.
      /// </summary>
      public Color? BackgroundColor
      {
         get { return this.m_BackgroundColor; }
         set
         {
            if (this.m_BackgroundColor != value)
            {
               this.m_BackgroundColor = value;
               this.Redraw ();
            }
         }
      }

      /// <summary>
      /// Gets or Sets the foreground color of the marker.
      /// </summary>
      public Color? ForegroundColor
      {
         get { return this.m_ForegroundColor; }
         set
         {
            if (this.m_ForegroundColor != value)
            {
               this.m_ForegroundColor = value;
               this.Redraw ();
            }
         }
      }
      
      /// <summary>
      /// Gets or Sets a tag for the marker.
      /// </summary>
      public object Tag { get; set; }

      /// <summary>
      /// Gets or Sets the tool tip of the marker.
      /// </summary>
      public object ToolTip { get; set; }
   }
}